//
//  FoodViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/27.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "FoodViewController.h"
#import "HX_Macro.h"
#import "UIView+Frame.h"
#import "HMZNavgationController.h"
#import "CommandViewController.h"
#import "FoodMakeViewController.h"
#import "CategoryViewController.h"
#import "FoodClassViewController.h"
#import "CategoryViewController.h"
#import "UIView+BundleExtension.h"
@interface FoodViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *arrTitles;
@property (nonatomic, strong) UIView *titleView;
//红色底部的view
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIViewController *showVc;
@property (nonatomic, strong) UIScrollView *contentView;
@end
@implementation FoodViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrTitles = [NSArray array];
    self.arrTitles = @[@"推荐", @"食材", @"分类"];
    [self addChildViewController];
    [self creatTitleBtn];
    [self creatLeftBtn];
    [self creatContentView];
}
- (void)addChildViewController {
    CommandViewController *command = [[CommandViewController alloc] init];
    [self addChildViewController:command];
    FoodMakeViewController *food = [[FoodMakeViewController alloc] init];
    [self addChildViewController:food];
    CategoryViewController *category = [[CategoryViewController alloc] init];
    [self addChildViewController:category];
}
- (void)creatTitleBtn {
    self.titleView = [[UIView alloc] init];
    _titleView.HX_height = 44;
    _titleView.HX_width = 200;
    [self addTitleView];
    self.navigationItem.titleView = _titleView;
}
- (void)addTitleView {
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor redColor];
    lineView.HX_height = 3;
    for (int i = 0; i < _arrTitles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(_titleView.frame.size.width / 3 * i, 0, _titleView.frame.size.width / 3, _titleView.frame.size.height - 3);
        [self.titleView addSubview:btn];
        [btn setTitle:_arrTitles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
        btn.tag = 1000 + i;
        [btn addTarget:self action:@selector(chilk:) forControlEvents:UIControlEventTouchUpInside];
        lineView.HX_y = btn.frame.size.height;
        if (i == 0) {
            //   让按钮的lable根据内容计算尺寸
            [btn.titleLabel sizeToFit];
            [self chilk:btn];
            lineView.HX_width = btn.titleLabel.HX_width;
            lineView.centerX = btn.center.x;
        }
    }
    [self.titleView addSubview:lineView];
    self.lineView = lineView;
}
- (void)chilk:(UIButton *)btn {
    self.selectBtn.enabled = YES;
    btn.enabled = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.HX_width = btn.titleLabel.HX_width;
        self.lineView.centerX = btn.centerX;
    }];
    self.selectBtn = btn;
    NSInteger i = btn.tag - 1000;
    CGPoint index = self.contentView.contentOffset;
    index.x = i * SCREEN_WIDTH;
    [self.contentView setContentOffset:index animated:YES];
}
- (void)creatContentView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    self.contentView = contentView;
    [self.view insertSubview:contentView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:contentView];
}
- (void)creatLeftBtn {
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"saoyisao"] forState:UIControlStateNormal];
    left.contentHorizontalAlignment = 1;
    left.HX_height = 30;
    left.HX_width = 30;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    [right setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    right.HX_width = 30;
    right.HX_height = 30;
    right.contentHorizontalAlignment = 2;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    NSLog(@"%ld", i);
    self.showVc = self.childViewControllers[i];
    _showVc.view.frame = CGRectMake(scrollView.contentOffset.x, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 44);
    [scrollView addSubview:_showVc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self chilk:_titleView.subviews[index]];
}
@end
